/*
* $Id: AddChangePointView.cpp 162 2010-10-04 11:21:18Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "AddChangePointView.h"
#include "Event/EventId.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"

#pragma warning(disable: 4407)

#define TITLE_ID		1
#define NAME_POINT_ID	2
#define NOTE_POINT_ID	3
#define USER_FIELD_ID	4

/************************************************************************/
/*                        CAddChangePointView                           */
/************************************************************************/
CAddChangePointView::CAddChangePointView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){
	IEventManager::GetEventManager()->SubscribeEvent(dynamic_cast<ISubscriber*>(this), POINT_STN_CHANGE_EVENT);
}
CAddChangePointView::~CAddChangePointView(){
	IEventManager::GetEventManager()->UnSubscribeEvent(dynamic_cast<ISubscriber*>(this), POINT_STN_CHANGE_EVENT);
}

void CAddChangePointView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(GetHeaderString(), TITLE_ID));
	CFamilyManager::point pnt = IJournalComponent::GetFamilyManager()->GetPoint(m_nIdParameter);
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), NAME_POINT_ID, editItem::TEXT, m_aCurValues[NAME_POINT_ID]));
	CEditorItem* pItemNote = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_POINT_ID, editItem::TEXT, m_aCurValues[NOTE_POINT_ID]);
	m_editorDrawer.AddItem(pItemNote);
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pItemNote->Hide(true);

	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::POINTS, USER_FIELD_ID, m_nIdRecord, m_aCurValues);
}

void CAddChangePointView::GetSaveData(CFamilyManager::point& pnt, std::map<std::tstring, std::tstring>& aUserField){
	pnt.m_strName = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	pnt.m_strNote = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		aUserField.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
}

void CAddChangePointView::GetCurValue(){
	m_aCurValues[NAME_POINT_ID] = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	m_aCurValues[NOTE_POINT_ID] = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		m_aCurValues[USER_FIELD_ID + i] = m_editorDrawer.GetValueItem(USER_FIELD_ID + i);
}

/************************************************************************/
/*                         CAddPointView                                */
/************************************************************************/
CAddPointView::CAddPointView(CState* pState)
: CAddChangePointView(pState){}
CAddPointView::~CAddPointView(){}
std::tstring CAddPointView::GetHeaderString(){
	return ILanguageManager::GetLanguageManager()->GetString(ADD_POINT_ID);
}
void CAddPointView::SetState(){
	m_pState->m_nState = Journal::VIEW_ADD_POINT;
	m_pState->m_nIdParameter = 0;
}
bool CAddPointView::IsViewState(State state){
	return state.m_type == Journal::ADD_POINT;
}

BEGIN_MESSAGE_MAP(CAddPointView, CAddChangePointView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CAddPointView::OnSavePoint(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::point pnt;
	std::map<std::tstring, std::tstring> aUserFields;
	GetSaveData(pnt, aUserFields);
	pManager->AddPoint(pnt, aUserFields);
	__super::OnCancel();
}

/************************************************************************/
/*                         CChangePointView                             */
/************************************************************************/
CChangePointView::CChangePointView(CState* pState)
: CAddChangePointView(pState){}
CChangePointView::~CChangePointView(){}
std::tstring CChangePointView::GetHeaderString(){
	return ILanguageManager::GetLanguageManager()->GetString(CHANGE_POINT_ID);
}
void CChangePointView::SetState(){
	m_pState->m_nState = Journal::VIEW_CHANGE_POINT;
}
bool CChangePointView::IsViewState(State state){
	return state.m_type == Journal::CHANGE_POINT;
}
void CChangePointView::InitCurrentValue(){
	CFamilyManager::point pnt = IJournalComponent::GetFamilyManager()->GetPoint(m_nIdParameter);
	m_aCurValues[NAME_POINT_ID] = pnt.m_strName;
	m_aCurValues[NOTE_POINT_ID] = pnt.m_strNote;
	m_nIdRecord = pnt.m_nIdRecord;
}

BEGIN_MESSAGE_MAP(CChangePointView, CAddChangePointView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CChangePointView::OnSavePoint(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::point pnt;
	std::map<std::tstring, std::tstring> aUserFields;
	GetSaveData(pnt, aUserFields);
	pManager->ChangePoint(m_nIdParameter, pnt, aUserFields);
	__super::OnCancel();
}